package com.miyuan.wm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.miyuan.wm.aop.UserOperation;
import com.miyuan.wm.mapper.SupplierMapper;
import com.miyuan.wm.pojo.SupplierEntity;
import com.miyuan.wm.request.AddSuppliersRequestVo;
import com.miyuan.wm.request.QuerySuppliersRequestVo;
import com.miyuan.wm.request.UpdateSuppliersRequestVo;
import com.miyuan.wm.response.BaseResponse;
import com.miyuan.wm.response.PageResponse;
import com.miyuan.wm.response.QuerySupplierByIdResponseVo;
import com.miyuan.wm.response.QuerySuppliersResponseVo;
import com.miyuan.wm.service.SupplierService;
import com.miyuan.wm.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName SupplierServiceImpl
 * @Description 供应商管理接口业务类
 */
@Service
@Slf4j
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, SupplierEntity> implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public PageResponse<List<QuerySuppliersResponseVo>> listSuppliers(QuerySuppliersRequestVo requestVo) {
        Page<SupplierEntity> page = new Page<>(requestVo.getPageNumber(), requestVo.getPageSize());
        Wrapper<SupplierEntity> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(requestVo.getName())) {
            wrapper.like("name", requestVo.getName());
        }
        if (StringUtils.isNotBlank(requestVo.getPersonInCharge())) {
            wrapper.like("person_in_charge", requestVo.getPersonInCharge());
        }
        if (StringUtils.isNotBlank(requestVo.getTel())) {
            wrapper.like("tel", requestVo.getTel());
        }
        wrapper.orderBy("id", false);
        List<SupplierEntity> supplierEntities = supplierMapper.selectPage(page, wrapper);
        List<QuerySuppliersResponseVo> responseVos = BeanUtils.convert(supplierEntities, QuerySuppliersResponseVo.class);
        return PageResponse.newInstance(responseVos, page.getTotal(), page.getCurrent(), page.getSize());
    }

    @Override
    @UserOperation("新增供应商信息")
    public BaseResponse<Boolean> addSuppliers(AddSuppliersRequestVo requestVo) {
        SupplierEntity entity = BeanUtils.convert(requestVo, SupplierEntity.class);
        supplierMapper.insert(entity);
        return BaseResponse.successOf(Boolean.TRUE);
    }

    @Override
    @UserOperation("修改供应商信息")
    public BaseResponse<Boolean> updateSuppliers(UpdateSuppliersRequestVo requestVo) {
        SupplierEntity entity = BeanUtils.convert(requestVo, SupplierEntity.class);
        entity.setId(requestVo.getId());
        supplierMapper.updateById(entity);
        return BaseResponse.successOf(Boolean.TRUE);
    }

    @Override
    @UserOperation("删除供应商信息")
    public BaseResponse<Boolean> deleteSuppliers(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return BaseResponse.errorOf(Boolean.FALSE);
        }
        supplierMapper.deleteBatchIds(ids);
        return BaseResponse.successOf(Boolean.TRUE);
    }

    @Override
    public BaseResponse<List<QuerySuppliersResponseVo>> querySuppliersForSelect() {
        Wrapper<SupplierEntity> wrapper = new EntityWrapper<>();
        List<SupplierEntity> supplierEntities = supplierMapper.selectList(wrapper);
        return BaseResponse.successOf(BeanUtils.convert(supplierEntities, QuerySuppliersResponseVo.class));
    }

    @Override
    public BaseResponse<QuerySupplierByIdResponseVo> querySupplierById(int id) {
        SupplierEntity supplierEntity = supplierMapper.selectById(id);
        QuerySupplierByIdResponseVo supplierByIdResponseVo = new QuerySupplierByIdResponseVo();
        supplierByIdResponseVo.setSupplierName(supplierEntity.getName());
        supplierByIdResponseVo.setSupplierAddress(supplierEntity.getAddress());
        supplierByIdResponseVo.setSupplierPersonInCharge(supplierEntity.getPersonInCharge());
        supplierByIdResponseVo.setSupplierTel(supplierEntity.getTel());
        return BaseResponse.successOf(supplierByIdResponseVo);
    }
}
